import( "io", true )
import("CArray");
import( "MPI" )

  id = MPI.Rank()
  size = MPI.Size()
  if(size != 2)
    throw("Its a pingpong program. Exact 2 players")
  
  
  for( i=Math.pow( 2, 10 ); i<=Math.pow( 2, 20 ); i*=2 )
  {
    sdata = new CArray.Integer( i )
    for( j=0; j<i; j++ )
      sdata[j] = j;
    
    MPI.Barrier()
    if(id == 0)
    {
      stime = MPI.Wtime()
      MPI.Send(sdata, 1)
      rdata = MPI.Recv(1)
      for (k=0; k<rdata.length; k++ )
        if(rdata[k] != k)
          print("rdata["+k+"]="+rdata[k]+"   Something wrong")
      ftime = MPI.Wtime()
      printf("Elapsed time for %d elements of integer is %e \n", i, (ftime-stime))
    }
    else
    {
      rdata = MPI.Recv(0)
      MPI.Send(rdata, 0)
    }
  }

